/**
 * Created by wdcy336 on 2015/6/8.
 */
Ext.define('Monkey.view.authentication.AuthController', {
    extend: 'Monkey.view.BaseController',
    alias: 'controller.ctrl-auth',

    onLoginClick: function () {
        var viewModel = this.getViewModel();
        //验证码校验
        this.validateCode(this.login);
    },

    validateCode: function (callback) {
        var view = this.getView();
        var viewModel = this.getViewModel();
        this.request(
            Monkey.URL.JCAPTCHA_VALIDATE_URL,
            'GET', {
                code: viewModel.getCode()
            },
            function (result) {
                if (!result.success) {
                    Ext.Msg.show({
                        title: '提示',
                        message: result.message,
                        buttons: Ext.Msg.OK,
                        icon: Ext.Msg.ERROR
                    });
                    return;
                }
                callback(view, viewModel);
            });
    },

    login: function (view, viewModel) {
        Monkey.Utils.request(
            Monkey.URL.LOGIN_URL,
            'POST', {
                account: viewModel.getAccount(),
                password: viewModel.getPassword(),
                rememberMe: viewModel.getRememberMe()
            },
            function (result) {
                if (!result.success) {
                    Ext.Msg.show({
                        title: '提示',
                        message: result.message,
                        buttons: Ext.Msg.OK,
                        icon: Ext.Msg.ERROR
                    });
                    return;
                }
                view.destroy();
                Ext.create('Monkey.view.main.Viewport');
            });
    },

    //重置登录表单
    resetForm: function () {
        var formPanel = this.getReferences().formPanel;
        formPanel.reset();
    },

    //enter事件
    enterEvent: function (field, e) {
        if (e.getKey() == Ext.EventObject.ENTER) {//响应回车
            this.onLoginClick();
        }
    }

});